package com.itheima.reggie.handler;

import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//全局异常处理类
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    //处理业务异常
    @ExceptionHandler(CustomException.class)
    public ResultInfo customException(Exception e) {
        e.printStackTrace();
        return ResultInfo.error(e.getMessage());
    }

    //处理菜品或套餐名重复异常
    @ExceptionHandler(DuplicateKeyException.class)
    public ResultInfo douException(Exception e) {
        e.printStackTrace();
        return ResultInfo.error("名称重复!");
    }

    //非预期异常
    @ExceptionHandler(Exception.class)
    public ResultInfo exceptionHandle(Exception e) {
        e.printStackTrace();
        return ResultInfo.error("服务器开小差了,请稍后再试!");
    }
}
